﻿// Copyright (C) 2012-2013 Weekend Game Studio
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

#region Using Statements
using System;
using WaveEngine.Common.Input;
using WaveEngine.Components.Animation;
using WaveEngine.Components.UI;
using WaveEngine.Framework;
using WaveEngine.Framework.Graphics;
using WaveEngine.Framework.Services;
#endregion

namespace AnimationSampleProject
{
    class PlayerBehavior : Behavior
    {
        private string[] animations = { "Attack1","Idle1","Idle2","Drag","Drop","Upgrade", "Stopped", "Hype","Victory", "Attack2", "Attack3", "GoToHype",};
        
        private int animationIndex;
        private ButtonState lastSpaceState;    
        private KeyboardState state;

        [RequiredComponent]
        public Transform3D playerTransform;

        [RequiredComponent]
        public Animation3D animation;

        #region Initialize

        /// <summary>
        /// Initializes a new instance of the <see cref="PlayerBehavior" /> class.
        /// </summary>
        public PlayerBehavior()
            : base("PlayerBehavior")
        {
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Allows this instance to execute custom logic during its <c>Update</c>.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        /// <remarks>
        /// This method will not be executed if the <see cref="T:WaveEngine.Framework.Component" />, or the <see cref="T:WaveEngine.Framework.Entity" />
        /// owning it are not <c>Active</c>.
        /// </remarks>
        protected override void Update(TimeSpan gameTime)
        {
            state = WaveServices.Input.KeyboardState;

            if (state.Left == ButtonState.Pressed)
                playerTransform.Rotation.Y -= 0.03f;
            if (state.Right == ButtonState.Pressed)
                playerTransform.Rotation.Y += 0.03f;

            if (lastSpaceState == ButtonState.Release && state.Space == ButtonState.Pressed)
            {
                EntityManager.Find<TextBlock>("Animation").Text = "Current Animation: " + animations[animationIndex];
                animation.PlayAnimation(animations[animationIndex], true);
                animationIndex++;
                if (animationIndex == animations.Length)
                    animationIndex = 0;
            }

            lastSpaceState = state.Space;
        }

        #endregion
    }
}
